package com.example.rocketdemo.producer;

import apache.rocketmq.v2.MessageOrBuilder;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author GuoAo
 * @className LogProducer
 * @date 2025/6/24
 * @description TODO
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class LogProducer {

    private final RocketMQTemplate template;

    public void send(String topic, String tag ,String message) {
        log.info("发送消息：{}", message);
        Map<String, Object> headers = new HashMap<>();
        headers.put("userId", UUID.randomUUID().toString());
        headers.put("type", "VIP");
        headers.put("tag","tag1");
        Message<String> msg = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.TAGS,"VIP").build();
        template.send(topic+":"+tag, msg);
    }

    public void sendTxMsg(String topic, String tag ,String message){
        Message<String> msg = MessageBuilder.withPayload(message).setHeader("TAGS", "plus").build();
        template.sendMessageInTransaction(topic+":"+tag, msg, null);

    }


}
